﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using personalplaner.common.mvvm;
using personalplaner.business.stamm;
using System.Collections.ObjectModel;
using personalplaner.common.mvvm.commands;
using System.Windows.Input;

namespace personalplaner.gui.common.viewmodels.models
{
    public class InitWizardTeamModel : BaseModel
    {
        public InitWizardTeamModel()
        {
            SetNewAbteilung();
        }

        private void SetNewAbteilung()
        {
            SelectedAbteilung = new Abteilung
            {
                EinsaetzeProTag = 1
            };
            RaiseMoveFocus("Name");
        }

        #region Commands
        
        ICommand _savecommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_savecommand == null)
                    _savecommand = new RelayCommand(OnSave, parameter => SelectedAbteilung != null && SelectedAbteilung.IsValid);
                return _savecommand;
            }
        }

        private void OnSave(object parameter)
        {
            Abteilungen.Add(SelectedAbteilung);
            ActiveGridItem = null;

            SetNewAbteilung();
        }

        ICommand _newcommand;
        public ICommand NewCommand
        {
            get
            {
                if (_newcommand == null)
                    _newcommand = new RelayCommand(OnNew);
                return _newcommand;
            }
        }

        private void OnNew(object parameter)
        {
            ActiveGridItem = null;

            SetNewAbteilung();
        }

        ICommand _deleteCommand;
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                    _deleteCommand = new RelayCommand(OnDelete, param => ActiveGridItem != null);
                return _deleteCommand;
            }
        }

        private void OnDelete(object parameter)
        {
            Abteilungen.Remove(ActiveGridItem);
            ActiveGridItem = null;

            SetNewAbteilung();
        }

        #endregion

        #region Properties

        IList<Abteilung> _abteilungen;
        public IList<Abteilung> Abteilungen
        {
            get
            {
                if (_abteilungen == null)
                    _abteilungen = new ObservableCollection<Abteilung>();
                return _abteilungen;
            }
        }

        Abteilung _activegriditem;
        public Abteilung ActiveGridItem
        {
            get
            {
                return _activegriditem;
            }
            set
            {
                _activegriditem = value;
                RaisePropertyChanged("ActiveGridItem");

				SelectedAbteilung = value;
            }
        }

        Abteilung _selectedAbteilung;
        public Abteilung SelectedAbteilung
        {
            get
            {
                return _selectedAbteilung;
            }
            set
            {
                _selectedAbteilung = value;
                RaisePropertyChanged("SelectedAbteilung");
                RaisePropertyChanged("IsEditEnabled");
            }
        }

        public bool IsEditEnabled
        {
            get
            {
                return SelectedAbteilung != null;
            }
        }

        #endregion
    }
}
